<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Frontend;
use app\common\library\Auth;
use app\common\model\Equipment;
use app\common\model\work\Repair;
use app\common\model\work\Service;
use app\common\model\work\Warranty as WarrantyModel;
use think\Db;
use think\Exception;
use think\Config;

/**
 * 二期接口
 */
class Index extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function test(){
        $list=\app\admin\model\Equipment::whereLike("image","https://ylsbnew.westts.cn%")->select();
        foreach ($list as $v){
            $image=substr($v["image"],25);
            $image="https://ylsb.westts.cn".$image;
            \app\admin\model\Equipment::where(["id"=>$v["id"]])->update(["image"=>$image]);
        }
        dump($list);die();
    }
    /**
     * 首页
     *
     */
    public function index()
    {
        $list=\app\common\model\work\Service::all();
        $time=time();
        if($list){
            foreach ($list as $v){
                $report=Db::name("report")->where(["warranty_id"=>$v["warranty_id"],"service_id"=>$v["id"]])->find();
                if(empty($report)){
                    $data[]=[
                        "warranty_id"=>$v["warranty_id"],
                        "service_id"=>$v["id"],
                        "image"=>$v["image"],
                        "createtime"=>$time,
                        "user_id"=>$v["user_id"],
                        "type_data"=>$v["type_data"],
                        "applytime"=>$v["applytime"],
                        "contract_status"=>$v["contract_status"],
                        "service_type"=>$v["service_type"],
                        "warranty_time"=>$v["warranty_time"],
                        "status"=>$v["status"],
                        "ending"=>$v["ending"],
                        "present_time"=>$v["present_time"],
                        "servicetime"=>$v["servicetime"],
                        "statementtime"=>$v["statementtime"],
                    ];
                }
            }
            if($data){
                Db::name("report")->insertAll($data);
            }
        }die();
        $id=$this->request->param("id");
        $this->assign("id",$id);
        return $this->view->fetch();
    }
    /**
     * 获取设备信息
     * */
    public function getEquipment(){
        if (!$this->request->post("id")) $this->error("id为空");
        $id=$this->request->post("id");
        $list=Equipment::with(["department"=>function($query){
            $query->withField('id,name,department_id');
        }])->field("equipment.*,b.name")
            ->join("hospital a","department.department_id=b.id")
            ->where(["equipment.id"=>$id])
        ->find();
        $this->success("成功",$list);
    }
    /**
     * 发送短信
     * */
    public function autoSend(){
        $list=\app\common\model\work\Warranty::where(["type_data"=>'0',"open_data"=>'1'])
            ->where("engineer_id >0 and sale_id >0")
            ->whereIn("status",['0','1'])
            ->select();
        if($list){
            foreach ($list as $v){
                $equipment=Equipment::get(["id"=>$v["equipment_id"]]);
                $repair=Repair::whereTime('createtime', '-1 days')->where(["warranty_id"=>$v["id"]])->find();
                if(empty($repair)){
                    //没填写维修记录
                    $user=\app\common\model\User::where(["id"=>$list["engineer_id"]])->find();
                    $sale=\app\common\model\User::where(["id"=>$list["engineer_id"]])->find();
                    $sms=new Sms();
                    $str=[
                        "wordID"=>$list["workID"],
                        "name"=>$equipment["name"],
                    ];
                    if($user && $user["mobile"]){
                        $ret = $sms->hairsendSms($user["mobile"],$str,'SMS_245150564');
                    }
                    if($sale && $sale["mobile"]){
                        $ret = $sms->hairsendSms($sale["mobile"],$str,'SMS_245150564');
                    }
                }
            }
            $this->success('成功');
        }
    }
    /**
     * 开票时间发送短信
     * 每天执行一次
     * */
    public function invoicing_note(){
        $warrantry=WarrantyModel::alias("a")
            ->field("a.id,b.name,d.name equipment_name,d.next_json")
            ->join("hospital b"," a.hospital_id=b.id")
            ->join("equipment d"," a.equipment_id=d.id")
            ->where(["open_data"=>'1'])
            ->where("JSON_CONTAINS(next_json, JSON_OBJECT('invoicing_time', '".date("Y-m-d",time())."'))")
            ->select();
        if ($warrantry){
            $sms = new Sms();
            $platform_mobile=\think\Config::get('site.platform_mobile');//平台电话
            foreach ($warrantry as $v){
                $str = [
                    "hospital_name" => $v["name"],
                    "equ_name" => $v["equipment_name"],
                ];
                $ret = $sms->hairsendSms($platform_mobile, $str, 'SMS_260470116');
            }
        }
    }

    /**
     * 质保发送短信
     * 每天凌晨00.00执行一次
     * */
    public function qa_note(){
        $qa_warrantry=WarrantyModel::field("workID,warranty_time")
            ->with(["sale"=>function ($query) {
                $query->withField('id,nickname');
            },"engineer"=>function ($query) {
                $query->withField('id,nickname');
            },])
            ->where(["qa_note"=>0,"type_data"=>0,"open_data"=>'1'])
            ->where(" UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME( warranty_time), '%Y-%m-%d')) = ".strtotime(date("Y-m-d",strtotime("+7 day",time()))))
            ->select();
        $platform_mobile=\think\Config::get('site.platform_mobile');//平台电话
        if(!empty($qa_warrantry)){
            $sms = new Sms();
            foreach ($qa_warrantry as $v){
                $str = [
                    "wordID" => $v["workID"],
                    "eng_name" => $v["engineer"]["nickname"],
                    "sale_name" => $v["sale"]["nickname"],
                ];
                //dump($str);
                WarrantyModel::where(["id"=>$v["id"]])->update(["qa_note"=>1]);
                $ret = $sms->hairsendSms($platform_mobile, $str, 'SMS_260580047');
            }
        }
        $qa_warrantry2=WarrantyModel::field("workID,warranty_time,id")
            ->with(["sale"=>function ($query) {
                $query->withField('id,nickname');
            },"engineer"=>function ($query) {
                $query->withField('id,nickname');
            },])
            ->where(["qa_note"=>0,"type_data"=>1,"open_data"=>'1'])
            ->select();//维保工单
        if(!empty($qa_warrantry2)){
            $sms = new Sms();
            foreach ($qa_warrantry2 as $v){
                $service=Service::where(["warranty_id"=>$v["id"]])
                    ->where(" UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME( servicetime), '%Y-%m-%d')) = ".strtotime(date("Y-m-d",strtotime("+7 day",time()))))//->where("servicetime > ".strtotime("+7 day"))
                    ->order("servicetime","desc")->find();
                if(!empty($service)){
                    $str = [
                        "wordID" => $v["workID"],
                        "eng_name" => $v["engineer"]["nickname"],
                        "sale_name" => $v["sale"]["nickname"],
                    ];
                    //dump($str);
                    WarrantyModel::where(["id"=>$v["id"]])->update(["qa_note"=>1]);
                    $ret = $sms->hairsendSms($platform_mobile, $str, 'SMS_260580047');
                }
            }
        }

        //到期前一周发短信给工程师
        $qa_warrantry3=WarrantyModel::field("workID,warranty_time,id")
            ->with(["engineer"=>function ($query) {
                $query->withField('id,nickname,mobile');
            },"hospital"=>function ($query) {
                $query->withField('id,name');
            },"equipment"=>function ($query) {
                $query->withField('id,name');
            }])
            ->where(["type_data"=>1,"open_data"=>'1'])
            ->select();//维保工单
        if(!empty($qa_warrantry3)){
            $sms = new Sms();
            foreach ($qa_warrantry3 as $v){
                $service=Service::where(["warranty_id"=>$v["id"]])
                    ->where(" UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME( servicetime), '%Y-%m-%d')) = ".strtotime(date("Y-m-d",strtotime("+7 day",time()))))//->where("servicetime > ".strtotime("+7 day"))
                    ->order("servicetime","desc")->find();
                if(!empty($service)){
                    $str = [
                        "workID" => $v["workID"],
                        "hospital_name" => $v["hospital"]["name"],
                        "equ_name" => $v["equipment"]["name"],
                    ];
                    //dump($v["engineer"]["mobile"]);
                    $ret = $sms->hairsendSms($v["engineer"]["mobile"], $str, 'SMS_263871112');
                }
            }
        }
    }


}
